﻿Imports System.ComponentModel.Composition
Imports System.Windows.Media
Imports Microsoft.VisualStudio.Text
Imports Microsoft.VisualStudio.Text.Classification
Imports Microsoft.VisualStudio.Utilities

''' <summary>
''' This class causes a classifier to be added to the set of classifiers. Since 
''' the content type is set to "text", this classifier applies to all text files
''' </summary>
<Export(GetType(IClassifierProvider))>
<ContentType("text")> 
Class EditorClassifier1Provider
    Implements IClassifierProvider
    
    ''' <summary>
    ''' Import the classification registry to be used for getting a reference
    ''' to the custom classification type later.
    ''' </summary>
    <Import()> 
    Private _classificationRegistry As IClassificationTypeRegistryService

	'returns an instance of the classifier
	Public Function GetClassifier(ByVal buffer As ITextBuffer) As IClassifier Implements IClassifierProvider.GetClassifier
        Return buffer.Properties.GetOrCreateSingletonProperty(Of EditorClassifier1)(Function() New EditorClassifier1(_classificationRegistry))
	End Function

End Class


''' <summary>
''' Classifier that classifies all spans as an instance of the EditorClassifier1Type
''' </summary>
Class EditorClassifier1
    Implements IClassifier
    ' This event gets raised if a non-text change would effect the classification in some way,
    ' for example typing /* would cause the classification to change in C# without directly
    ' affecting the span.
    Public Event ClassificationChanged As EventHandler(Of ClassificationChangedEventArgs) Implements IClassifier.ClassificationChanged
    Private ReadOnly _classificationType As IClassificationType

    Friend Sub New(ByVal registry As IClassificationTypeRegistryService)
        _classificationType = registry.GetClassificationType("EditorClassifier1")
    End Sub

    ''' <summary>
    ''' This method scans the given SnapshotSpan for potential matches for this classification.
    ''' In this instance, it classifies everything and returns each span as a new ClassificationSpan.
    ''' </summary>
    ''' <param name="trackingSpan">The span currently being classified</param>
    ''' <returns>A list of ClassificationSpans that represent spans identified to be of this classification</returns>
    Public Function GetClassificationSpans(ByVal span As SnapshotSpan) As IList(Of ClassificationSpan) Implements IClassifier.GetClassificationSpans
        Dim UseText As String = span.ToString

        Dim Search As New System.Text.RegularExpressions.Regex("Class\s+(?<Class>\w+)")
        Dim ClassName = Search.Match(UseText)



        Dim ClassIndex = UseText.IndexOf(ClassName.ToString)
        'Dim Name = UseText.Split({"Class ", "Class "}, StringSplitOptions.RemoveEmptyEntries).Last.Trim


        Dim classifications As List(Of ClassificationSpan)

   

        '   Return Nothing
        If ClassIndex > -1 AndAlso ClassName.Success Then
            'Dim T2 As New ClassificationSpan(New SnapshotSpan(span.Snapshot, New Span(Search2, Search3)), _classificationType)
            'create a list to hold the results
            Dim Snapp As New SnapshotSpan(span.Start + 7, span.End)

            'New ClassificationSpan(New SnapshotSpan(Snapp.Snapshot, New Span(ClassIndex, ClassIndex + ClassName.ToString.Length)), _classificationType)
            classifications = New List(Of ClassificationSpan) From {
            New ClassificationSpan(New SnapshotSpan(Snapp.Snapshot, New Span(0, ClassName.Value.Replace("Class", "").Length)), _classificationType)
            }
        Else
            classifications = New List(Of ClassificationSpan) From {
                New ClassificationSpan(New SnapshotSpan(span.Snapshot, New Span(0, 0)), _classificationType)
            }
        End If




        'Dim List As New List(Of ClassificationSpan) From 

        'Dim T = New ClassificationSpan(New SnapshotSpan(span.Snapshot, New Span(span.Start, span.Length)), _classificationType)



        Return classifications
    End Function



End Class
